<?php

namespace app\index\controller;
use app\index\model\Complain as ComplainModel;
use think\Request;

class Complain extends Base
{
    //投诉列表
    public function  complainList()
    {
        //获取所有投诉表数据
        $complainList = ComplainModel::all();

        //获取记录数量
        $count = ComplainModel::count();

        $this -> view -> assign('complainList', $complainList);
        $this -> view -> assign('count', $count);

        return $this -> view -> fetch('complain_list');
    }

    //投诉状态变更
    public function setStatus(Request $request)
    {
        //获取当前的投诉ID
        $complain_id = $request -> param('id');

        //查询
        $result = ComplainModel::get($complain_id);

        //启用和禁用处理
        if($result->getData('status') == 1) {
            ComplainModel::update(['status'=>0],['id'=>$complain_id]);
        } else {
            ComplainModel::update(['status'=>1],['id'=>$complain_id]);
        }
    }

    //渲染投诉编辑界面
    public function complainEdit(Request $request)
    {
        //获取到要编辑的投诉ID
        $complain_id = $request -> param('id');

        //根据ID进行查询
        $result = ComplainModel::get($complain_id);

        //给当前页面seo变量赋值
        $this->view->assign('title','编辑投诉');
        $this->view->assign('keywords','rice.cn');
        $this->view->assign('desc','水稻投诉管理系统');

        //给当前编辑模板赋值
        $this->view->assign('complain_info',$result);

        //渲染编辑模板
        return $this->view->fetch('complain_edit');
    }

    //投诉更新
    public function doEdit(Request $request)
    {
        //从提交表单中排除关联字段complain字段
        //$data = $request -> except('complain');
        $data = $request -> param(); // 如果全部获取,提交会提示缺少字段complain

        //设置更新条件
        $condition = ['id'=>$data['id']];

        //更新当前记录
        $result = ComplainModel::update($data,$condition);

        //设置返回数据
        $status = 0;
        $message = '更新失败,请检查';

        //检测更新结果,将结果返回给complain_edit模板中的ajax提交回调处理
        if (true == $result) {
            $status = 1;
            $message = '恭喜, 更新成功~~';
        }
        return ['status'=>$status, 'message'=>$message];
    }

    //渲染投诉添加界面
    public function complainAdd()
    {
        //给模板赋值seo变量
        $this->view->assign('title','编辑投诉');
        $this->view->assign('keywords','rice.cn');
        $this->view->assign('desc','水稻投诉管理系统');

        //渲染添加模板
        return $this->view->fetch('complain_add');
    }

    //添加投诉
    public function doAdd(Request $request)
    {
        //从提交表单中获取数据
        $data = $request -> param();

        //更新当前记录
        $result = ComplainModel::create($data);

        //设置返回数据的初始值
        $status = 0;
        $message = '添加失败,请检查';

        //检测更新结果,将结果返回给complain_edit模板中的ajax提交回调处理
        if (true == $result) {
            $status = 1;
            $message = '恭喜, 添加成功~~';
        }

        //自动转为json格式返回
        return ['status'=>$status, 'message'=>$message];
    }

    //软删除操作
    public function deleteComplain(Request $request)
    {
        $user_id = $request -> param('id');
        ComplainModel::update(['is_delete'=>1],['id'=> $user_id]);
        ComplainModel::destroy($user_id);

    }

    //恢复删除操作
    public function unDelete()
    {
        ComplainModel::update(['delete_time'=>NULL],['is_delete'=>1]);
    }

}